﻿using NLog;

namespace YS.K12.Core.IoT.Plugin
{
    /// <summary>
    ///服务插件
    /// </summary>
    public class ServerPlugin : PluginBase, IServerStartedPlugin, IServerStopedPlugin
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
        public Task OnServerStarted(IServiceBase sender, ServiceStateEventArgs e)
        {
            if (sender is ITcpService service)
            {
                foreach (var item in service.Monitors)
                {
                    _logger.Info($"iphost={item.Option.IpHost}");
                }
            }
            if (e.ServerState == ServerState.Running)
            {
                _logger.Info($"服务器成功启动");
            }
            else
            {
                _logger.Error($"服务器启动失败，状态：{e.ServerState}，异常：{e.Exception}");
            }
            return e.InvokeNext();
        }


        public Task OnServerStoped(IServiceBase sender, ServiceStateEventArgs e)
        {
            //这里可以增加告警
            _logger.Error("服务已停止");
            return e.InvokeNext();
        }
    }
}
